package com.example.client;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
 

import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;



import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
 
public class MainActivity extends Activity {
	String arr[]={
			 "Admin",
			 "Student",
			 };

	private Socket client;
	 private PrintWriter printwriter;
	 private EditText txtUserName;
	 private EditText txtPassWord;
	 private Socket s;
	 private PrintWriter p;	
	 private Button button;
	 private String username;
     private String password;
     private String role;
     private int x  =0;
     private static Vector content=new Vector<String>();
     private static Vector questionContent=new Vector<String>();
     private static Vector topic=new Vector<String>();
     private static Vector answer=new Vector<String>();
     private static Vector correct=new Vector<String>();
     private static Vector questionSize=new Vector<String>();
     public static boolean isToastShown=false;
     CorrectAnswer status=new CorrectAnswer();
     @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txtUserName = (EditText) findViewById(R.id.username); //reference to the text field
		txtPassWord = (EditText) findViewById(R.id.password); //reference to the text field
		final RadioButton rd2=(RadioButton)findViewById(R.id.rdExamination);
		rd2.setChecked(true);
		button = (Button) findViewById(R.id.button1);   //reference to the send button
	//	TextView txtText=(TextView)findViewById(R.id.textView2); 
	//	txtText.setText(status.statusTest+"");
		if(status.statusTest==1)
		 {
			 button.setEnabled(false);
		 }
		  //Button press event listener
		  button.setOnClickListener(new View.OnClickListener() {
		 
		   public void onClick(View v) {
		 CorrectAnswer s=new CorrectAnswer();
		    username = txtUserName.getText().toString(); //get the text message on the text field
		    password=txtPassWord.getText().toString();
		    s.username=username;
		    s.password=password;
		    txtUserName.setText("");      //Reset the text field to blank
		    txtPassWord.setText("");
		    try {
	            //
	            // Create a connection to the server socket on the server application
	            //er
	            //InetAddress host = InetAddress.getLocalHost();
	            Socket socket = new Socket("192.168.0.115", 7777);
	            
	            //
	            // Send a message to the client application
	            //
	            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
	            oos.writeObject(username + " ");
	            oos.flush();
	            oos.writeObject(password);
	            oos.flush();
	            oos.writeObject(role);
	            oos.flush();
	            oos.writeObject("showTopic");
	            oos.flush();
	            oos.writeObject("");
	            oos.flush();
	            oos.writeObject("");
	            oos.flush();
	            oos.writeObject("");
	            oos.flush();
	            oos.writeObject("");
	            oos.flush();
	            oos.writeObject("");
	            oos.flush();
	            oos.writeObject("");
	            oos.flush();
	            oos.writeObject("");
	            oos.flush();
	       
	            //
	            // Read and display the response message sent by server application
	            //
	            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
	            String message = (String) ois.readObject();
	            CorrectAnswer con=new CorrectAnswer();
	            	if(username.equals("")&&password.equals(""))
	            	{
	              		Toast.makeText(MainActivity.this, "Wrong UserName or PassWord or Role.Please try again", Toast.LENGTH_LONG).show();
	          	            		
	            	}
	            	else if(username.equals(""))
	            	{
	               		Toast.makeText(MainActivity.this, "Wrong UserName or PassWord or Role.Please try again", Toast.LENGTH_LONG).show();
  	            		
	         	       
	            	}
	            	else if(password.equals(""))
	            	{
	            		Toast.makeText(MainActivity.this, "Wrong UserName or PassWord or Role.Please try again", Toast.LENGTH_LONG).show();
	  	            	
	            	}
	            	else if(message.equals("Admin"))
	            	{
	       /*     		 String size = (String) ois.readObject();
		                 
		            	 for(int i=0;i<Integer.parseInt(size);i++)
		 	            {
		 	            	questionContent.add((String) ois.readObject());
		 	            	System.out.println(questionContent);
		 	            	
		 	            	con.setQuestionContent(questionContent);
		 	            }
		 	            
		 */            
	            		con.topic.removeAllElements();
	            	
	         String size3 = (String) ois.readObject();
	      	           
	      for(int i=0;i<Integer.parseInt(size3);i++)
          {
          	topic.add((String) ois.readObject());
          	con.setTopic(topic);
         	    
          }
	         

	            		 Intent intent=new Intent(MainActivity.this,Admin.class);
		            	 
		            	 startActivity(intent);
		    
	            	}
	            	else{
	            if(message.equals("Student"))
	            {
	            	 String size = (String) ois.readObject();
	                 	
	            	 String size1 = (String) ois.readObject();
	                  
	            	 String size2 = (String) ois.readObject();
	            	 String size3 = (String) ois.readObject();
	            	 for(int i=0;i<Integer.parseInt(size);i++)
	 	            {
	 	            	content.add((String) ois.readObject());
	 	            	
	 	            	
	 	            	con.setContent(content);
	 	            }
	            
	  	                   
	 	         	 for(int i=0;i<Integer.parseInt(size1);i++)
		 	            {
		 	            	answer.add((String) ois.readObject());
		 	            	con.setAnswer(answer);
		 	            	
		 		 	       
		 	            }
	 	          	 for(int i=0;i<Integer.parseInt(size2);i++)
		 	            {
		 	            	correct.add((String) ois.readObject());
		 	            	con.setCorrectPosition(correct);
			            	    
		 	            }
	 	          	 topic.removeAllElements();
	 	         	 for(int i=0;i<Integer.parseInt(size3);i++)
		 	            {
		 	            	topic.add((String) ois.readObject());
		 	            	con.setTopic(topic);
			            	    
		 	            }
	 	          	 //check();
		 	   		 if(status.statusTest==1)
		 	   		 {
		 	   			//button = (Button) findViewById(R.id.button1);
		 	   			//button.setEnabled(false);
		 	   		 }
		 	   		status.statusTest=1;
	            	 Intent intent=new Intent(MainActivity.this,Topic.class);
	            	 
	            	 startActivity(intent);
	    
	            }
	            else
	            {
	        		Toast.makeText(MainActivity.this, "Wrong UserName or PassWord or Role.Please try again", Toast.LENGTH_LONG).show();
	        		
	            }
	            	}
	            //Reset the text field to blank
			    
	            ois.close();
	            oos.close();
	        } catch (UnknownHostException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        }
	    }
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		   
		  });
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		 Spinner spin=(Spinner) findViewById(R.id.spinner1);
		 ArrayAdapter<String> adapter=new ArrayAdapter<String>
		 (
		 this,android.R.layout.simple_spinner_item,arr);
		 adapter.setDropDownViewResource
		 (android.R.layout.simple_list_item_single_choice);
		 spin.setAdapter(adapter);
			 spin.setOnItemSelectedListener(new MyProcessEvent());
		 }
		 private class MyProcessEvent implements
		 OnItemSelectedListener
		 {
	
		 public void onItemSelected(AdapterView<?> arg0,
		 View arg1,
		 int arg2,
		 long arg3) {
		//	 selection.setText(arr[arg2]);
			 role=arr[arg2];
		 }
		 public void onNothingSelected(AdapterView<?> arg0) {
	//	 selection.setText("");
		 }
		
		 
		
		
		
		
		
		
		
		
		
		}
/*
     void check()
	  {
    	
	  RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
	  
		final RadioButton rd1=(RadioButton)findViewById(R.id.rdPractical);
		final RadioButton rd2=(RadioButton)findViewById(R.id.rdExamination);
	
	  int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
	   
	  String radioButtonSelected = " ";
	 	
	  switch (checkedRadioButton) {
	    case R.id.rdPractical : radioButtonSelected ="rdPractical";
	    {
	      status.statusTest=0;
	  
	    }	
	    
	             break;
	    case R.id.rdExamination : radioButtonSelected = "rdExamination";
	    {
	    	  status.statusTest=1;
	    	  	
	    }
	         break;

	  }
	    
	    
	    	  
	  
	  }  
	  
*/
     
     @Override
         public void onBackPressed() {
    	// super.onBackPressed();
    	// android.os.Process.killProcess(android.os.Process.myUid());
        // super.finish();
       
    	 
              }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
